package com.etc.controller;

import com.etc.model.Result;
import com.etc.model.User;
import com.etc.model.esuser;
import com.etc.service.UserService;
import com.etc.utils.CaptchaUtil;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "cookie",method = RequestMethod.POST)
    public void cookie(User user, HttpServletRequest request,HttpServletResponse response) throws IOException {

        //根据用户名查询出用户所有信息
        Result byUsername = userService.findByUsername(user,request);
        //以流的形式输出
        PrintWriter writer = response.getWriter();
        //如果状态码等于600返回1
        if (byUsername.getStatus() == 600){
            writer.print(1);
        }
        //如果状态码等于200
        if(byUsername.getStatus() == 200){
            //从request获取到session
            HttpSession session = request.getSession();
            //登陆成功后把该用户放入session
            session.setAttribute("user", byUsername);
            //登陆成功跳转index
            writer.print(2);

        }else{
            //登陆失败
            writer.print(3);

        }

        writer.flush();
    }
    @RequestMapping("index")
    public String index(){

        return "index";
    }
    @RequestMapping("/index_home")
    public String index_home(){
        return "index_home";
    }


    @RequestMapping(value = "add",method = RequestMethod.GET)
    public String add(){
        return "zhuce";
    }

    @RequestMapping(value = "insert",method = RequestMethod.POST)
    public void insert(PrintWriter out,
                       @RequestParam(value = "id")String id,
                       @RequestParam(value = "username")String username,
                       @RequestParam(value = "password")String password) throws IOException {
        Integer save = userService.save(new User(id,username, password));
        if(save == 2){
            out.print(2);
        }else{
            out.print(1);
        }
    }
    @RequestMapping("delete")
    public void delete(PrintWriter out,String id){
        Integer delete = userService.delete(id);
        if(delete == 2){
            out.print(2);
        }else{
            out.print(1);
        }

    }
    @RequestMapping("product_Manage")
    public String product_Manage(HttpServletRequest request){
        List<User> users = userService.userByAll();
        request.setAttribute("userlist",users);
        return "product_Manage";
    }
    @RequestMapping("select")
    public esuser selectId(String id){
        esuser select = userService.select(id);
        return select;
    }
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public void update(PrintWriter out,
                       @RequestParam(value = "id")String id,
                       @RequestParam(value = "username")String username,
                       @RequestParam(value = "password")String password) throws Exception {
        Integer update = userService.update(new User(id, username, password));
        if(update == 2){
            out.print(2);
        }else{
            out.print(1);
        }
    }

    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    @ResponseBody
    public void captcha(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        CaptchaUtil.outputCaptcha(request, response);

    }
}
